package cc.sunni.test;

import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 收集器
 *
 * @author jl
 * @since 2021/1/17 14:19
 */
public class CollectDemo {

    public static void main(String[] args) {
        List<Student> students = Arrays.asList(new Student("小明", 20, 1, "三班"),
                new Student("大明", 21, 0, "四班"),
                new Student("小红", 22, 0, "四班"),
                new Student("大红", 23, 1, "五班")
        );

        // 得到所有学生的年龄
        List<Integer> ages = students.stream().map(Student::getAge).collect(Collectors.toList());
        System.out.println(ages);

        // 统计汇总信息
        IntSummaryStatistics ageSummaryStatistics = students.stream().collect(Collectors.summarizingInt(Student::getAge));
        System.out.println("年龄汇总信息:" + ageSummaryStatistics);

        // 分块(特殊的分组,只有2种)
        Map<Boolean, List<Student>> sex = students.stream().collect(Collectors.partitioningBy(s -> s.getSex() == 1));
        System.out.println("学生性别信息:" + sex);

        // 分组
        Map<String, List<Student>> grade = students.stream().collect(Collectors.groupingBy(Student::getGrade));
        System.out.println("学生班级信息:" + grade);

        // 得到班级人数
        Map<String, Long> count = students.stream().collect(Collectors.groupingBy(Student::getGrade, Collectors.counting()));
        System.out.println("班级人数信息:" + count);
    }
}
